package com.googlecode.grt192.HH11.deploy;

import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;

import java.io.IOException;

import javax.swing.JFrame;

import com.googlecode.grt192.HH11.actuator.ISolenoid;
import com.googlecode.grt192.HH11.actuator.PneumaticActuatorDisplay;
import com.googlecode.grt192.HH11.actuator.RPCSolenoidValve;
import com.googlecode.grt192.HH11.controller.HHController;
import com.googlecode.grt192.HH11.mechanism.HHMechanism;
import com.googlecode.grt192.HH11.sensor.HHSafety;
import com.googlecode.grt192.HH11.sensor.HHToggleSwitch;
import com.googlecode.grt192.HH11.sensor.IHHSwitch;
import com.googlecode.grt192.HH11.sensor.SwitchDisplay;
import com.googlecode.grtframework.rpc.InternetRPC;
import com.googlecode.grtframework.rpc.USBRPC;
import com.googlecode.grtframework.sensor.RPCSwitch;
import com.googlecode.grtframework.vis.Displayer;
import com.googlecode.grtframework.vis.MountedPosition;
import com.googlecode.grtframework.vis.RootMount;

/**
 * Only simulation.
 * 
 * @author ajc
 * 
 */
public class Main {

	public static final String CRIO_ADDRESS = "10.1.92.2";
	public static final int CRIO_RPC_PORT = 180;

	public static final String USB_PORT = "/dev/ttyACM0";

	public static final boolean USING_USB = true;

	public static final int NUM_MECHS = 8;

	/**
	 * Buttons
	 */
	// For all arrays: 0-7 are for mechanisms, 8 and 9 are for auto and manual
	public static final int AUTO_BUTTON = 8;
	public static final int MASTER_BUTTON = 9;

	public static final String[] MECHANISM_NAMES = new String[] { "spider",
			"skull", "head", "pumpkin", "monkey", "witch", "tentacles",
			"snake", "auto", "master" };

	public static final int X = 0;
	public static final int Y = 1;
	public static final int[][] BUTTON_POSITIONS = new int[][] { { 350, 150 },
			{ 375, 200 }, { 325, 200 }, { 250, 200 }, { 200, 200 },
			{ 125, 200 }, { 75, 200 }, { 100, 150 }, { 300, 100 }, { 100, 100 } };
	public static int[] BUTTON_RPC_KEYS = new int[] { 5, 6, 7, 8, 9, 10, 11,
			12, 13, 14, 15 };

	/**
	 * Actuators
	 */
	public static final int[] ACTUATOR_RPC_KEY = { 193, 194, 195, 196, 197,
			198, 199, 200 };
	public static final int[][] ACTUATOR_POSIITIONS = new int[][] { { 50, 40 },
			{ 50, 70 }, { 50, 100 }, { 50, 130 }, { 50, 160 }, { 50, 190 },
			{ 50, 220 }, { 50, 250 } };
	public static final double[] ACTUATOR_ORIENTATION = new double[] {
			Math.PI / 9, Math.PI / 9, Math.PI / 9, Math.PI / 9, Math.PI / 9,
			Math.PI / 9, Math.PI / 9, Math.PI / 9 };
	public static final int CHARACTERSTIC_LENGTH = 0;
	public static final int CHARACTERSTIC_WIDTH = 1;
	public static final int CHARACTERSTIC_EXTEND_LENGTH = 2;
	public static final int CHARACTERSTIC_RETRACT_TIME = 3;
	public static final int CHARACTERSTIC_CONTRACT_TIME = 4;
	public static final int[][] ACTUATOR_CHARACTERISTICS = new int[][] {
			{ 200, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 },
			{ 300, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 },
			{ 300, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 },
			{ 300, 20, 200, 1, 2 }, { 300, 20, 200, 1, 2 } };
	public static final boolean[] ACTUATOR_DEFAULT_STATE = { true, true, true,
			true, true, true, true, true };

	public static final int[][] MECHANISM_WAIT_TIMES =
	// floor 1

	// retract time is the time stays out, stays retracted
	// minC, maxC, minR, maxR
	{ { 2000, 5000, 3000, 5000 },// spider
			{ 5000, 10000, 1000, 1000 },// skull
			{ 5000, 10000, 2000, 2000 },// head
			{ 4000, 6000, 4000, 6000 },// pumpkin
			// 2nd story
			{ 1000, 5000, 1000, 5000 },// monkey
			{ 1000, 1000, 1000, 1000 },// witch
			{ 5000, 10000, 1000, 1000 },// tentacles

			// constant 1s extend time
			{ 5000, 6000, 1000, 1000 } };// snake

	public static void main(String[] args) throws InterruptedException {

		InternetRPC crioConn = null;
		crioConn = new InternetRPC(CRIO_ADDRESS, CRIO_RPC_PORT);
		USBRPC usbConn = null;
		if (USING_USB) {
			try {
				usbConn = new USBRPC(USB_PORT);
				// usbConn.start();
			} catch (NoSuchPortException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PortInUseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			usbConn.start();
		}

		/**
		 * House Display Display
		 */
		Displayer houseDisplay = new Displayer();
		houseDisplay.startPaintLoop();

		JFrame house = new JFrame("Haunted House 2011: House");
		house.getContentPane().add(houseDisplay);
		house.setVisible(true);
		house.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/**
		 * Switch Display
		 */
		Displayer switchDisplay = new Displayer();
		switchDisplay.startPaintLoop();

		JFrame switchboard = new JFrame("Haunted House 2011: Switchboardv1");
		switchboard.getContentPane().add(switchDisplay);
		switchboard.setVisible(true);
		switchboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		/**
		 * Master button: started and initialized
		 */
		MountedPosition masterButtonPos = new MountedPosition(RootMount.get(),
				BUTTON_POSITIONS[MASTER_BUTTON][X],
				BUTTON_POSITIONS[MASTER_BUTTON][Y], 0);
		RPCSwitch masterRPC = new RPCSwitch(usbConn,
				BUTTON_RPC_KEYS[MASTER_BUTTON]);
		SwitchDisplay masterButtonDisplay = new SwitchDisplay(masterRPC,
				masterButtonPos, MECHANISM_NAMES[MASTER_BUTTON]);
		masterRPC.startListening();
		masterButtonDisplay.startListening();
		switchDisplay.addDisplayable(masterButtonDisplay);

		/**
		 * Auto Button: started and initialized
		 */
		MountedPosition autoButtonPos = new MountedPosition(RootMount.get(),
				BUTTON_POSITIONS[AUTO_BUTTON][X],
				BUTTON_POSITIONS[AUTO_BUTTON][Y], 0);
		RPCSwitch autoRPC = new RPCSwitch(usbConn, BUTTON_RPC_KEYS[AUTO_BUTTON]);

		SwitchDisplay autoButtonDisplay = new SwitchDisplay(autoRPC,
				autoButtonPos, MECHANISM_NAMES[AUTO_BUTTON]);
		autoRPC.startListening();
		autoButtonDisplay.startListening();
		switchDisplay.addDisplayable(autoButtonDisplay);

		/**
		 * RPC SWITCHES
		 */
		RPCSwitch[] rpcSwitches = new RPCSwitch[NUM_MECHS];
		for (int i = 0; i < NUM_MECHS; i++) {
			rpcSwitches[i] = new RPCSwitch(usbConn, BUTTON_RPC_KEYS[i]);
			rpcSwitches[i].startListening();
		}

		/**
		 * Mechanism buttons : started and initialized
		 */
		MountedPosition[] buttonPositions = new MountedPosition[NUM_MECHS];
		SwitchDisplay[] buttonDisplays = new SwitchDisplay[NUM_MECHS];
		for (int i = 0; i < buttonPositions.length; i++) {
			// TODO match real positions
			// TODO labels
			buttonPositions[i] = new MountedPosition(RootMount.get(),
					BUTTON_POSITIONS[i][X], BUTTON_POSITIONS[i][Y], 0);

			// rpcSwitches[i];
			buttonDisplays[i] = new SwitchDisplay(rpcSwitches[i],
					buttonPositions[i], MECHANISM_NAMES[i]);
			buttonDisplays[i].startListening();
			switchDisplay.addDisplayable(buttonDisplays[i]);
		}

		/**
		 * RPC SOLENOIDS
		 */
		RPCSolenoidValve[] solenoids = new RPCSolenoidValve[NUM_MECHS];
		for (int i = 0; i < NUM_MECHS; i++) {
			solenoids[i] = new RPCSolenoidValve(crioConn, ACTUATOR_RPC_KEY[i]);
		}

		/**
		 * Solenoids
		 */

		MountedPosition[] solenoidPoses = new MountedPosition[NUM_MECHS];
		PneumaticActuatorDisplay[] actuators = new PneumaticActuatorDisplay[NUM_MECHS];
		for (int i = 0; i < solenoidPoses.length; i++) {
			solenoidPoses[i] = new MountedPosition(RootMount.get(),
					ACTUATOR_POSIITIONS[i][X], ACTUATOR_POSIITIONS[i][Y],
					ACTUATOR_ORIENTATION[i]);
			actuators[i] = new PneumaticActuatorDisplay(solenoidPoses[i],
					MECHANISM_NAMES[i],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_LENGTH],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_WIDTH],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_EXTEND_LENGTH],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_RETRACT_TIME],
					ACTUATOR_CHARACTERISTICS[i][CHARACTERSTIC_CONTRACT_TIME],
					ACTUATOR_DEFAULT_STATE[i]);
			actuators[i].start();
			houseDisplay.addDisplayable(actuators[i]);
		}

		/**
		 * Safety
		 */
		HHSafety safety = new HHSafety(masterRPC);
		safety.startListening();

		/**
		 * Mechanism code
		 */
		IHHSwitch[] switches = new IHHSwitch[NUM_MECHS];
		HHMechanism[] mechanisms = new HHMechanism[NUM_MECHS];

		HHController[] controllers = new HHController[NUM_MECHS];
		for (int i = 0; i < NUM_MECHS; i++) {
			switches[i] = new HHToggleSwitch(autoRPC, rpcSwitches[i]);
			switches[i].startListening();

			mechanisms[i] = new HHMechanism(new ISolenoid[] { actuators[i],
					solenoids[i] }, safety);
			mechanisms[i].startListening();

			controllers[i] = new HHController(mechanisms[i], switches[i],
					MECHANISM_WAIT_TIMES[i]);
			controllers[i].startListening();
		}
	}
}
